<?php

namespace app\admin\controller;

//require_once '../../../vendor/qiniu/autoload.php';
use app\BaseController;
use think\Container;
use think\facade\Request;
use think\facade\Db;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
class Oss extends Container
{
    // www.penyou.com/admin/Oss
    public static function upload($file)
    {
        // 用于签名的公钥和私钥
        $accessKey = 'nddwxXWjOnfufs65sQybZt3Twv_X5YZbUr6xI5U5';
        $secretKey = 'QN4LaxyMZbzLizQujUsZ9-DhwtS8pg1GprHAK9_m';
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        // 空间名  https://developer.qiniu.io/kodo/manual/concepts
        $bucket = 'penyou520';
        // 生成上传Token
        $token = $auth->uploadToken($bucket);

        // 构建 UploadManager 对象
        $uploadMgr = new UploadManager();
//        $file='ali_notify_log.txt';
        $key ='goods/ali_notify_log2.txt';  //文件名
        list($ret, $err) = $uploadMgr->putFile($token, $key, $file);
        print_r($ret);
        print_r($err);
        if ($err !== null) {
            $array=array('code'=>'1','msg'=>"上传失败",'data'=>[]);
        } else {
            $array=array('code'=>'0','msg'=>"上传成功",'data'=>$ret);
        }
        echo json_encode($array);
    }

}
